package com.sqx.modules.app.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sqx.common.utils.DateUtils;
import com.sqx.common.utils.Result;
import com.sqx.modules.app.annotation.Login;
import com.sqx.modules.app.entity.ServiceTime;
import com.sqx.modules.app.service.ServiceTimeService;
import com.sqx.modules.orders.dao.OrdersDao;
import com.sqx.modules.orders.entity.Orders;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;


@RestController
@RequestMapping("/serviceTime")
@Api(value = "预约时间", tags = {"预约时间"})
public class ServiceTimeController {

    @Autowired
    private ServiceTimeService serviceTimeService;
    @Autowired
    private OrdersDao ordersDao;

    @PostMapping("/insertServiceTime")
    @ApiOperation("添加预约时间")
    public Result insertServiceTime(@RequestBody ServiceTime serviceTime){
        ServiceTime oldServiceTime = serviceTimeService.selectServiceTime(serviceTime.getServiceTime(), serviceTime.getCompanyId());
        if(oldServiceTime!=null ){
            return Result.error("已经有相同的时间段了！");
        }
        serviceTime.setCreateTime(DateUtils.format(new Date()));
        serviceTimeService.save(serviceTime);
        return Result.success();
    }


    @PostMapping("/updateServiceTime")
    @ApiOperation("修改预约时间")
    public Result updateServiceTime(@RequestBody ServiceTime serviceTime){
        ServiceTime oldServiceTime = serviceTimeService.selectServiceTime(serviceTime.getServiceTime(), serviceTime.getCompanyId());
        if(oldServiceTime!=null && !oldServiceTime.getServiceTimeId().equals(serviceTime.getServiceTimeId())){
            return Result.error("已经有相同的时间段了！");
        }
        serviceTimeService.updateById(serviceTime);
        return Result.success();
    }


    @GetMapping("/deleteServiceTime")
    @ApiOperation("删除预约时间")
    public Result deleteServiceTime(Long serviceTimeId){
        serviceTimeService.removeById(serviceTimeId);
        return Result.success();
    }

    @GetMapping("/selectServiceTime")
    @ApiOperation("查询预约时间")
    public Result selectServiceTime(Long serviceTimeId,Long userId){
        return Result.success().put("data",serviceTimeService.list(new QueryWrapper<ServiceTime>().eq(serviceTimeId!=null,"service_time_id",serviceTimeId).eq("company_id",userId).orderByAsc("sort")));
    }

    @GetMapping("/selectOrderServiceTime")
    @ApiOperation("查询预约时间")
    public Result selectServiceTime(Integer page,Integer limit,Long userId,Long id,String time,Long serviceTimeId){
        if(page==null || limit==null){
            List<ServiceTime> list = serviceTimeService.list(new QueryWrapper<ServiceTime>().eq(serviceTimeId!=null,"service_time_id",serviceTimeId).eq("company_id", userId).orderByAsc("sort"));
            if(id!=null){
                for(ServiceTime serviceTime:list){
                    String startTime=time+" "+serviceTime.getServiceTime();
                    Integer counts = ordersDao.selectCount(new QueryWrapper<Orders>().in("state", 1, 2, 4).eq("order_taking_user_id", userId).eq("order_taking_id",id).eq("start_time", startTime));
                    if(serviceTime.getNum()>counts){
                        serviceTime.setIsMax(0);
                    }else{
                        serviceTime.setIsMax(1);
                    }
                }
            }
            return Result.success().put("data",list);
        }else{
            IPage<ServiceTime> list = serviceTimeService.page(new Page<>(page,limit),new QueryWrapper<ServiceTime>().eq("company_id", userId).orderByAsc("sort"));
            return Result.success().put("data",list);
        }
    }

}
